home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 May / PCPlus May 1998=disk A.iso / full / CBUILDER / SAMS / SAMPLES / CHAP04 / AIRPLANE.CPP next >
Encoding:
C/C++ Source or Header  |  1997-02-12  |  2.7 KB  |  142 lines

  1. #include <condefs.h>
  2. #include <iostream.h>
  3. #include "airplane.h"
  4. #include <stdio.h>
  5. //
  6. // Constructor performs initialization
  7. //
  8. Airplane::Airplane(const char* _name, int _type) :
  9.   type(_type),
  10.   status(ONRAMP),
  11.   speed(0),
  12.     altitude(0),
  13.   heading(0)
  14. {
  15.     switch (type) {
  16.       case AIRLINER : ceiling = 35000; break;
  17.     case COMMUTER : ceiling = 20000; break;
  18.     case PRIVATE  : ceiling = 8000;
  19.   }
  20.   name = new char[50];
  21.   strcpy(name, _name);
  22. }
  23.  
  24. //
  25. // Destructor performs cleanup.
  26. //
  27. Airplane::~Airplane()
  28. {
  29.     delete[] name;
  30. }
  31.  
  32. //
  33. // Gets a message from the user.
  34. //
  35. bool
  36. Airplane::SendMessage(int msg, char* response,
  37.     int spd, int dir, int alt)
  38. {
  39.     //
  40.   // Check for bad commands.
  41.   //
  42.   if (spd > 500) {
  43.       strcpy(response, "Speed cannot be more than 500.");
  44.     return false;
  45.   }
  46.   if (dir > 360) {
  47.       strcpy(response, "Heading cannot be over 360 degrees.");
  48.     return false;
  49.   }
  50.   if (alt < 100 && alt != -1) {
  51.       strcpy(response, "I'd crash, bonehead!");
  52.     return false;
  53.   }
  54.   if (alt > ceiling) {
  55.       strcpy(response, "I can't go that high.");
  56.       return false;
  57.   }
  58.   //
  59.   // Do something base on which command was sent.
  60.   //
  61.     switch (msg) {
  62.       case MSG_TAKEOFF : {
  63.         // Can't take off if already in the air!
  64.         if (status != ONRAMP) {
  65.         strcpy(response, "I'm already in the air!");
  66.           return false;
  67.       }
  68.       TakeOff(dir);
  69.       break;
  70.     }
  71.     case MSG_CHANGE : {
  72.         // Can't change anything if on the ground.
  73.       if (status == ONRAMP) {
  74.           strcpy(response, "I'm on the ground");
  75.         return false;
  76.       }
  77.          // Only change if a non-negative value was passed.
  78.         if (spd != -1) speed = spd;
  79.       if (dir != -1) heading = dir;
  80.       if (alt != -1) altitude = alt;
  81.             status == CRUISING;
  82.         break;
  83.     }
  84.     case MSG_LAND : {
  85.         if (status == ONRAMP) {
  86.           strcpy(response, "I'm already on the ground.");
  87.         return false;
  88.       }
  89.       Land();
  90.       break;
  91.       }
  92.     case MSG_REPORT : ReportStatus();
  93.   }
  94.   //
  95.   // Standard reponse if all went well.
  96.   //
  97.   strcpy(response, "Roger.");
  98.     return true;
  99. }
  100.  
  101. //
  102. // Perform takeoff.
  103. //
  104. void
  105. Airplane::TakeOff(int dir)
  106. {
  107.     heading = dir;
  108.   status = TAKINGOFF;
  109. }
  110.  
  111. //
  112. // Perform landing.
  113. //
  114. void
  115. Airplane::Land()
  116. {
  117.     speed = heading = altitude = 0;
  118.   status == ONRAMP;
  119. }
  120.  
  121. //
  122. // Build a string to report the airplane's status.
  123. //
  124. int
  125. Airplane::GetStatus(char* statusString)
  126. {
  127.     sprintf(statusString, "%s, Altitude: %d, Heading: %d, "
  128.       "Speed: %d\n", name, altitude, heading, speed);
  129.   return status;
  130. }
  131.  
  132. //
  133. // Get the status string and output it to the screen.
  134. //
  135. void
  136. Airplane::ReportStatus()
  137. {
  138.     char buff[100];
  139.   GetStatus(buff);
  140.   cout << endl << buff << endl;
  141. }
  142.